//@Lim Wei Ke A0083402R
package logic;

import java.util.ArrayList;

import storage.Storage;
import global.CommandType.Type;
import global.Constant;
import global.Task;

public class UpdateCommand extends Command {

	private int index;
	private Task task;
	private boolean isExecutable;
	private String feedback;

	public UpdateCommand(int index, Task task, boolean isExecutable, String feedback) {
		this.index = index;
		this.task = task;
		this.isExecutable = isExecutable;
		this.feedback = feedback;
	}

	public ArrayList<String> execute(){
		ArrayList<String> newFeedback = new ArrayList<String>();

		if (isExecutable) {
			if (index <= ExecutionHandler.getDisplayFileNames().size()){
				try {
					Task oldTask;
					ArrayList<Task> oldTasks = new ArrayList<Task>();
					String oldTaskFileName = ExecutionHandler.getDisplayFileNames().get(index - 1);
					oldTask = Storage.getFileContentAsTask(oldTaskFileName);


					if (task.getTaskType() == null){
						task.setTaskType(oldTask.getTaskType());
					} else {
						if (task.getTaskType().equals("time") && oldTask.getTaskType().equals("deadline") ){
							Long diff = task.getEndTime().getTime() - task.getStartTime().getTime();
							if (diff == 3600000) {
								task.setTaskType("deadline");
								task.setEndTime(task.getStartTime());
								task.setStartTime(null);
							}	
						}
					}
					if (task.getEndTime() == null)
						task.setEndTime(oldTask.getEndTime());
					if (task.getStartTime() == null)
						task.setStartTime(oldTask.getStartTime());
					if (task.getTaskLocation() == null)
						task.setTaskLocation(oldTask.getTaskLocation());
					if (task.getTaskName() == null || task.getTaskName().equals(Constant.EMPTY_STRING))
						task.setTaskName(oldTask.getTaskName());

					if (oldTask.getTaskStatus())
						task.markDone();
					else
						task.markUnDone();

					if (oldTask.getSyncStatus())
						task.setSync();
					else
						task.setUnSync();

					if (oldTask.getTaskType().equalsIgnoreCase(Constant.FLOAT_TASK_TYPE))
						Storage.deleteFileWithoutRecord(oldTask.getTaskFileName());
					else
						Storage.deleteFile(oldTask.getTaskFileName());

					Storage.addFile(task);

					oldTasks.add(task);					
					ExecutionHandler.setOldTasks(oldTasks);

					ArrayList<Task> updatedTasks = new ArrayList<Task>();
					updatedTasks.add(oldTask);
					ExecutionHandler.setUpdatedTasks(updatedTasks);

					DisplayCommand displayLeft = new DisplayCommand(task);
					newFeedback = displayLeft.displayNewlyAddedTask(index);

					feedback += "Task updated successfully.\n";
					ExecutionHandler.setMostRecentCommandType(Type.UPDATE);
					ExecutionHandler.setIsUndo(false);
				} catch(Exception e) {
					newFeedback.add("Updating Task " + task.getTaskName() + " unsuccessful.\n");
				}
			} else {
				newFeedback.add("Illegal index " + index + " entered.\n");
			}
		}
		newFeedback.add(feedback);
		return newFeedback;
	}
}
//@Lim Wei Ke A0083402R